<?php
header("Content-type:text/html;charset=utf-8");
$arr = array();
if(file_get_contents('php://input')){
    include '../../api/config.php';
    include './comm.php';
    $data = json_decode(file_get_contents('php://input'));
    $head = $data->head;
    if($head === 5000){
        if(flag($data)){
            $arr['data'] = array();
            $sql_1 = "SELECT sum(num) FROM user_log";
            $stmt = $conn->query($sql_1);
            $stmt->execute();
            $result = $stmt->fetchAll();
            $arr['data']['all'] = $result[0]['sum(num)'];

            $sql_2 = "SELECT count(distinct ip) FROM user_log GROUP BY ip";
            $stmt = $conn->query($sql_2);
            $stmt->execute();
            $result = $stmt->fetchAll();
            $arr['data']['ip_all'] = count($result);

            $sql_3 = "SELECT count(*) FROM user_log WHERE DATE_FORMAT(times,'%Y-%m') = DATE_FORMAT(CURDATE(),'%Y-%m')";
            $stmt = $conn->query($sql_3);
            $stmt->execute();
            $result = $stmt->fetchAll();
            $arr['data']['m_all'] = $result[0]['count(*)'];

            echo get_msg(200,'获取成功~','../index.html',$arr['data']);
        }else{
            echo get_msg(500,'权限不足~',false,false);
        };
    };//获取总访问量

    if($head === 5001){
        if(flag($data)){
            $arr['data'] = array();
            $sql_1 = "SELECT source FROM user_log";
            $stmt = $conn->query($sql_1);
            $stmt->execute();
            $result = $stmt->fetchAll();
            $arr['data']['source'] = $result;

            $sql_2 = "SELECT bro FROM user_log";
            $stmt = $conn->query($sql_2);
            $stmt->execute();
            $result = $stmt->fetchAll();
            $arr['data']['bro'] = $result;

            $sql_3 = "SELECT os FROM user_log";
            $stmt = $conn->query($sql_3);
            $stmt->execute();
            $result = $stmt->fetchAll();
            $arr['data']['os'] = $result;

            echo get_msg(200,'获取成功~','../index.html',$arr['data']);
        }else{
            echo get_msg(500,'权限不足~','../index.html',false);
        };
    };//获取浏览器等

    if($head === 5002){
        if(flag($data)){
            $arr['data'] = array();
            $sql_1 = "SELECT id,ip,times,num FROM user_log WHERE YEARWEEK(date_format(times,'%Y-%m-%d'),1) = YEARWEEK(now(),1)";
            $stmt = $conn->query($sql_1);
            $stmt->execute();
            $result = $stmt->fetchAll();
            $arr['data']['all'] = $result;

            $sql_2 = "SELECT id,times,count(distinct ip) FROM user_log GROUP BY ip";
            $stmt = $conn->query($sql_2);
            $stmt->execute();
            $result = $stmt->fetchAll();
            for($i = 0; $i < count($result); $i++){
                $result[$i]['num'] = $result[$i]['count(distinct ip)'];
            };
            $arr['data']['ip'] = $result;



            echo get_msg(200,'获取成功~','../index.html',$arr['data']);
        }else{
            echo get_msg(500,'权限不足~','../index.html',false);
        };
    };//获取周访问

    if($head === 5003){
        if(flag($data)){
            $type = $data->type;
            $len = $data->nums;
            $arr['data'] = array();
            if($type === 1){
                $sql_1 = "SELECT id,title,href,ip,num,city,times FROM user_log WHERE  TO_DAYS(times) = TO_DAYS(NOW()) ORDER BY num DESC";
            };
            if($type === 2){
                $sql_1 = "SELECT id,title,href,ip,num,city,times FROM user_log WHERE YEARWEEK(date_format(times,'%Y-%m-%d')) = YEARWEEK(now()) ORDER BY num DESC";
            };
            if($type === 3){
                $sql_1 = "SELECT id,title,href,ip,num,city,times FROM user_log WHERE  date_format(times,'%Y-%m')=date_format(now(),'%Y-%m') ORDER BY num DESC";
            };
            $stmt = $conn->query($sql_1);
            $stmt->execute();
            $result = $stmt->fetchAll();
            if($len < count($result)){
                for($i = 0; $i < $len; $i++){
                    array_push($arr['data'],$result[$i]);
                };
            }else{
                $arr['data'] = $result;
            };
            echo get_msg(200,'获取成功~','../index.html',$arr['data']);
        }else{
            echo get_msg(500,'权限不足~','../index.html',false);
        };
    };//获取排行

    
    if($head === 5004){
        if(flag($data)){
            $arr['data'] = array();
            $sql_1 = "SELECT id,city,num FROM user_log";
            $stmt = $conn->query($sql_1);
            $stmt->execute();
            $result = $stmt->fetchAll();
            $arr['data'] = $result;

            echo get_msg(200,'获取成功~','../index.html',$arr['data']);
        }else{
            echo get_msg(500,'权限不足~','../index.html',false);
        };
    };//获取用户分布
}else{
    echo get_msg(500,'权限不足~','../index.html',false);
};